﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200030B RID: 779
	internal class StartWithObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06001060 RID: 4192 RVA: 0x0004B958 File Offset: 0x00049D58
		public StartWithObservable(IObservable<T> source, T value) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.value = value;
		}

		// Token: 0x06001061 RID: 4193 RVA: 0x0004B974 File Offset: 0x00049D74
		public StartWithObservable(IObservable<T> source, Func<T> valueFactory) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.valueFactory = valueFactory;
		}

		// Token: 0x06001062 RID: 4194 RVA: 0x0004B990 File Offset: 0x00049D90
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new StartWithObservable<T>.StartWith(this, observer, cancel).Run();
		}

		// Token: 0x0400099C RID: 2460
		private readonly IObservable<T> source;

		// Token: 0x0400099D RID: 2461
		private readonly T value;

		// Token: 0x0400099E RID: 2462
		private readonly Func<T> valueFactory;

		// Token: 0x0200030C RID: 780
		private class StartWith : OperatorObserverBase<T, T>
		{
			// Token: 0x06001063 RID: 4195 RVA: 0x0004B99F File Offset: 0x00049D9F
			public StartWith(StartWithObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001064 RID: 4196 RVA: 0x0004B9B0 File Offset: 0x00049DB0
			public IDisposable Run()
			{
				T value;
				if (this.parent.valueFactory == null)
				{
					value = this.parent.value;
				}
				else
				{
					try
					{
						value = this.parent.valueFactory();
					}
					catch (Exception error)
					{
						try
						{
							this.observer.OnError(error);
						}
						finally
						{
							base.Dispose();
						}
						return Disposable.Empty;
					}
				}
				this.OnNext(value);
				return this.parent.source.Subscribe(this.observer);
			}

			// Token: 0x06001065 RID: 4197 RVA: 0x0004BA58 File Offset: 0x00049E58
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06001066 RID: 4198 RVA: 0x0004BA68 File Offset: 0x00049E68
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001067 RID: 4199 RVA: 0x0004BAA0 File Offset: 0x00049EA0
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400099F RID: 2463
			private readonly StartWithObservable<T> parent;
		}
	}
}
